"""
Case Type   : GUC
Case Name   : 验证alter system set方式设置enable_partition_opfusion，合理报错
Create At   : 2024/10/14
Owner       : nanyang12
Description :
        1.查询enable_partition_opfusion默认值=off
        2.使用alter system set方式设置参数enable_partition_opfusion为on
        3.清理环境，恢复参数默认值
Expect      :
        1.默认值"enable_partition_opfusion=off"
        2.合理报错
        3.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
commonsh = CommonSH('dbuser')


class DeveloperOptions(unittest.TestCase):
    def setUp(self):
        self.constant = Constant()
        self.user_node = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0123start----')

    def test_post_auth_delay(self):
        LOG.info('--步骤1:查询enable_partition_opfusion默认值为off--')
        sql_cmd = commonsh.execut_db_sql('''show enable_partition_opfusion;''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        LOG.info('--步骤2:使用alter system set方式设置参数enable_partition_opfusion为on;expect:合理报错--')
        sql_cmd = commonsh.execut_db_sql('''alter system set 
                    enable_partition_opfusion to on;
                    ''')
        LOG.info(sql_cmd)
        self.assertIn("ERROR:  unsupport parameter: enable_partition_opfusion\nALTER SYSTEM SET only support "
                      "POSTMASTER-level, SIGHUP-level and BACKEND-level guc variable", sql_cmd)

    def tearDown(self):
        LOG.info('--步骤3:恢复默认值--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_partition_opfusion=off')
        LOG.info(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status in status)
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0123finish----')
